﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using LdExpress;
using System.Data;
using MySql.Data.MySqlClient;

public partial class CreateCusdoc : System.Web.UI.Page
{
    public string strCondition = "";
    public int NumOfItemsPerPage = 20;
    public string strPager = "";
    public string strError = "";
    public string strMess = "";
    public int nPermissions = 0;
    public string strLetter = "letter";
    public string strAlphaBet = "";
    string strChar = "";
    public int spdVOL_Sum = 0;
    public double spdGW_Sum = 0;
    public double spdMEAS_Sum = 0;
    protected void Page_Load(object sender, EventArgs e)
    {
        int nPage = 1;
        DataTable dtList;
        int nNumOfPages = 0;
        if (Session["logined_id"] != null)
        {
            if (Session["cusdoc"] != "") nPermissions = int.Parse(Session["cusdoc"].ToString());

        }
        else
            Response.Redirect("Login.aspx");
        if (!Page.IsPostBack)
        {
            //Get MBLSHPR
            if (nPermissions < 3) btnSave.Visible = false;
            if (Request.QueryString["ID"] == null)
            {
                string MBLSHPR_Province = "";
                Tinh objTinh = new Tinh(long.Parse(Session["UserProvince"].ToString()));
                objTinh.Load(GV.cn, GV.trans);
                if (objTinh.Ma.ToUpper() == "05")
                    MBLSHPR_Province = "Bach Viet Shipping Co., ltd	\n" +
                                      "7th, 65B Ton Duc Thang Str, Dong Da Dist, Ha noi, Viet Nam\n" +
                                      "Tel: 08443 732 4889	Fax: 08443 732 4890";
                else if (objTinh.Ma.ToUpper() == "03")
                    MBLSHPR_Province = "Bach viet Shipping Co., Ltd - HCM Branch\n" +
                                       "3rd, 101 Le Quoc Hung Str, 4 Dist, HCM, Viet Nam\n" +
                                       "Tel: 08483 943 2330	Fax: 08483 943 2331";
                txtMBLSHPR.Text = MBLSHPR_Province;
                txtFILENO.Text = CusdocModel.GetNewFileNo(GV.cn, GV.trans).ToString();
                txtHBLNO.Text = "00" + txtFILENO.Text.ToUpper();
            }

            if (!String.IsNullOrEmpty(Request.QueryString["action"]))
            {
                //delete Shipping Detail
                if (Request.QueryString["action"].ToString() == "deleteSPR")
                {
                    ShippingDetail obj = new ShippingDetail();
                    obj.ID = long.Parse(Request.QueryString["shpID"].ToString());
                    //obj.Load(GV.cn, GV.trans);
                    obj.Delete(GV.cn, GV.trans);
                    Response.Redirect("CreateCusDoc.aspx?ID=" + Request.QueryString["ID"].ToString());
                }
                //delete Tracking
                if (Request.QueryString["action"].ToString() == "deleteTrk")
                {
                    Tracking obj = new Tracking();
                    obj.ID = long.Parse(Request.QueryString["trkID"].ToString());
                    //obj.Load(GV.cn, GV.trans);
                    obj.Delete(GV.cn, GV.trans);
                    Response.Redirect("CreateCusDoc.aspx?ID=" + Request.QueryString["ID"].ToString());
                }
            }
            txtFILENO.ReadOnly = true;
            BuildSPDList(strCondition, -1, nPage, out nNumOfPages, out dtList, null, null);
            rptShippingDetail.DataSource = dtList;
            rptShippingDetail.DataBind();

            BuildTrackingList(strCondition, -1, nPage, out nNumOfPages, out dtList, null, null);
            rptTracking.DataSource = dtList;
            rptTracking.DataBind();
            LoadCusDoc();
        }


    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        long cusdocid = 0;
        if (!string.IsNullOrEmpty(Request.QueryString["ID"]))
        {
            cusdocid = long.Parse(Request.QueryString["ID"].ToString());
        }
        string FILENO = txtFILENO.Text.Trim();
        string HBLNO = txtHBLNO.Text;
        if (Request.QueryString["ID"] == null)
        {
            FILENO = CusdocModel.GetNewFileNo(GV.cn, GV.trans).ToString();
            HBLNO = "00" + FILENO;
        }

        string MBKGNO = txtMBKGNO.Text.Trim();
        string MBLNO = txtMBLNO.Text.ToUpper();
        string HBLSHPR = txtHBLSHPR.Text;
        string HBLCNEE = txtHBLCNEE.Text;
        string HBLNTF = txtHBLNTF.Text;
        string CARRCODE = txtCARRCODE.Text;
        string SERIBILL = txtSERIBILL.Text;

        string VSSL = txtVSSL.Text;
        string VOYNO = txtVOYNO.Text;
        int PORID = 0;//dia diem nhan hang
        string PORNAME = txtPORID.Text;
        int POLID = 0;//cang chat hang
        string POLNAME = txtPOLID.Text;
        DateTime SHONBOARD = DateTime.Now;//ngay len tau
        if (txtSHONBOARD.Text.Length > 0)
        {
            SHONBOARD = DateTime.Parse(txtSHONBOARD.Text);
        }
        int PodID = 0;//cang giao hang
        string PODNAME = txtPODID.Text;//EXT - Ten cang giao hang
        int DESTID = 0;// cang den
        string DESTNAME = txtDESTID.Text;//EXT - Ten cang den
        int PayAbleaId = 0;
        string PAYABLEATNAME = txtPAYABLEAT.Text;//noi thanh toan cuoc
        int PoissId = 0;
        string POISSNAME = txtPOISS.Text;//noi phat hanh bill
        string condition = "";
        if (!string.IsNullOrEmpty(PORNAME))
            condition += "'" + DBModule.RefineString(PORNAME) + "'";
        if (!string.IsNullOrEmpty(POLNAME))
        {
            if (condition.Length > 0)
                condition += ",'" + DBModule.RefineString(POLNAME) + "'";
            else
                condition += "'" + DBModule.RefineString(POLNAME) + "'";
        }
        if (!string.IsNullOrEmpty(PODNAME))
        {
            if (condition.Length > 0)
                condition += ",'" + DBModule.RefineString(PODNAME) + "'";
            else
                condition += "'" + DBModule.RefineString(PODNAME) + "'";
        }
        if (!string.IsNullOrEmpty(DESTNAME))
        {
            if (condition.Length > 0)
                condition += ",'" + DBModule.RefineString(DESTNAME) + "'";
            else
                condition += "'" + DBModule.RefineString(DESTNAME) + "'";
        }
        if (!string.IsNullOrEmpty(PAYABLEATNAME))
        {
            if (condition.Length > 0)
                condition += ",'" + DBModule.RefineString(PAYABLEATNAME) + "'";
            else
                condition += "'" + DBModule.RefineString(PAYABLEATNAME) + "'";
        }
        if (!string.IsNullOrEmpty(POISSNAME))
        {
            if (condition.Length > 0)
                condition += ",'" + DBModule.RefineString(POISSNAME) + "'";
            else
                condition += "'" + DBModule.RefineString(POISSNAME) + "'";
        }



        if (condition.Length > 0) condition = " and tbl_ports.name in (" + condition + ")";
        int nNumOfPages = 0;
        System.Data.DataSet dsport = new System.Data.DataSet();

        LdExpress.Ports.GetList(condition, "", -1, 1, out nNumOfPages, out dsport, LdExpress.GV.cn, LdExpress.GV.trans);
        var portArr = (from System.Data.DataRow item in dsport.Tables[0].Rows
                       select new { ID = int.Parse(item["ID"].ToString()), Name = item["Name"] }).ToList();

        var porttoitem = portArr.Where(p => p.Name.ToString().ToLower() == POLNAME.ToString().ToLower()).FirstOrDefault();

        if (porttoitem != null)
        {
            POLID = porttoitem.ID;
        }
        //Response.Write("POLID="+POLID.ToString());
        porttoitem = portArr.Where(p => p.Name.ToString().ToLower() == PORNAME.ToString().ToLower()).FirstOrDefault();

        if (porttoitem != null)
        {
            PORID = porttoitem.ID;
        }

        porttoitem = portArr.Where(p => p.Name.ToString().ToLower() == PODNAME.ToString().ToLower()).FirstOrDefault();

        if (porttoitem != null)
        {
            PodID = porttoitem.ID;
        }

        porttoitem = portArr.Where(p => p.Name.ToString().ToLower() == DESTNAME.ToString().ToLower()).FirstOrDefault();

        if (porttoitem != null)
        {
            DESTID = porttoitem.ID;
        }

        porttoitem = portArr.Where(p => p.Name.ToString().ToLower() == PAYABLEATNAME.ToString().ToLower()).FirstOrDefault();

        if (porttoitem != null)
        {
            PayAbleaId = porttoitem.ID;
        }

        porttoitem = portArr.Where(p => p.Name.ToString().ToLower() == POISSNAME.ToString().ToLower()).FirstOrDefault();

        if (porttoitem != null)
        {
            PoissId = porttoitem.ID;
        }



        string DOGMN = txtDOGMN.Text;//dien giai hang hoa
        string TypeOfMovement = txtTypeOfMovement.Text;//dieu kien van chuyen
        string TOP = ddTOP.SelectedValue;//loai cuoc thanh toan        
        string NOORI = ddNOORI.Text;//so luong van don goc
        DateTime DOISS = DateTime.Now;//txtDOISS.Text;//ngay phat hanh bill
        if (txtDOISS.Text.Trim().Length > 0)
        {
            DOISS = DateTime.Parse(txtDOISS.Text);
        }
        else
            DOISS = SHONBOARD;
        string AGDLVR = txtAGDLVR.Text;//ten dai ly
        string CARRN = txtCARRN.Text;//ten hang tau

        string MBLSHPR = txtMBLSHPR.Text;//ten nguoi gui hang tren MBL
        string MBLCNEE = txtMBLCNEE.Text; //AGDLVR; //txtMBLCNEE.Text;//ten nguoi nhan hang tren MBL
        string MBLNTF = txtMBLNTF.Text;//ten nguoi thong bao tren MBL
        string BOOKER = txtBOOKER.Text;//nguoi dat cho
        int CTRNO = 0;//so hop dong
        int.TryParse(txtCTRNO.Text, out CTRNO);
        DateTime CTRVALID = DateTime.Now;//ngay het han hd
        if (txtCTRVALID.Text.Length > 0)
        {
            CTRVALID = DateTime.Parse(txtCTRVALID.Text);
        }
        string CTRNAME = txtCTRNAME.Text;//ten hop dong

        double RATE = 0;// gia cuoc
        double.TryParse(txtRATE.Text, out RATE);
        string UNIT = txtUNIT.Text;//don vi
        string CURR = txtCURR.Text;//loai tien
        double DVALUE = 0;//tong gia tri
        double.TryParse(txtDVALUE.Text, out DVALUE);
        string STUFF = txtSTUFF.Text;//dong hang
        string TOPMBL = ddTOPMBL.SelectedValue;

        CusdocModel cus = new CusdocModel();

        if (cusdocid > 0)
        {
            cus.ID = cusdocid;
            cus.Load(GV.cn, GV.trans);
        }
        else
        {
            cus.CreatedDate = DateTime.Now;
        }

        cus.AGDLVR = AGDLVR.ToUpper();
        cus.BOOKER = BOOKER.ToUpper();
        cus.CARRN = CARRN.ToUpper();
        cus.CTRNAME = CTRNAME.ToUpper();
        cus.CTRNO = CTRNO;
        cus.CTRVALID = CTRVALID;
        cus.CURR = CURR.ToUpper();
        cus.DOGMN = DOGMN.ToUpper();
        cus.DOISS = DOISS;
        cus.DVALUE = DVALUE;
        cus.FILENO = long.Parse(FILENO);
        cus.HBLCNEE = HBLCNEE.ToUpper();
        cus.HBLNO = HBLNO.ToUpper();
        cus.HBLNTF = HBLNTF.ToUpper();
        cus.HBLSHPR = HBLSHPR.ToUpper();
        cus.MBKGNO = MBKGNO.ToUpper();
        cus.MBLCNEE = MBLCNEE.ToUpper();
        cus.MBLNO = MBLNO.ToUpper();
        cus.MBLNTF = MBLNTF.ToUpper();
        cus.MBLSHPR = MBLSHPR.ToUpper();
        cus.NOORI = NOORI.ToUpper();
        if (PayAbleaId > 0)
            cus.PAYABLEAT = PayAbleaId;
        if (PoissId > 0)
            cus.POISS = PoissId;
        if (PodID > 0)
            cus.PodID = PodID;
        if (POLID > 0)
            cus.POLID = POLID;
        if (PORID > 0)
            cus.PORID = PORID;
        if (DESTID > 0)
            cus.DESTID = (int)DESTID;
        cus.RATE = RATE;
        cus.SHONBOARD = SHONBOARD;
        cus.STUFF = STUFF.ToUpper();
        cus.TOP = TOP.ToUpper();
        cus.TypeOfMovement = TypeOfMovement.ToUpper();
        cus.UNIT = UNIT.ToUpper();
        cus.UserID = long.Parse(Session["logined_id"].ToString());
        cus.VOYNO = VOYNO.ToUpper();
        cus.VSSL = VSSL.ToUpper();
        cus.TOPMBL = TOPMBL.ToUpper();
        cus.CARRCODE = CARRCODE.ToUpper();
        cus.SERIBILL = SERIBILL.ToUpper();
        if (txtBACKDATE.Text.Trim().Length > 0)
        {
            cus.BACKDATE = DateTime.Parse(txtBACKDATE.Text);
        }
        else
            cus.BACKDATE = DateTime.MinValue;
        try
        {
            cus.MBLType = int.Parse(cbMBLTYPE.SelectedValue);
        }
        catch
        {
            cus.MBLType = 0;
        }

        try
        {
            cus.HBLType = int.Parse(cbHBLTYPE.SelectedValue);
        }
        catch
        {
            cus.HBLType = 0;
        }
        cus.NOTE = txtNote.Text.ToUpper();
        //EXT POST NAME
        cus.PORNAME = PORNAME.ToUpper();
        cus.POLNAME = POLNAME.ToUpper();
        cus.PODNAME = PODNAME.ToUpper();
        cus.DESTNAME = DESTNAME.ToUpper();
        cus.PAYABLEATNAME = PAYABLEATNAME.ToUpper();
        cus.POISSNAME = POISSNAME.ToUpper();
        cus.Save(GV.cn, GV.trans);

        cusdocid = cus.ID;

        //Save ShippingDetail
        foreach (RepeaterItem item in rptShippingDetail.Items)
        {
            int sdID = 0;
            if (!string.IsNullOrEmpty(((HiddenField)item.FindControl("txtID")).Value.Trim()))
            {
                sdID = int.Parse(((HiddenField)item.FindControl("txtID")).Value.Trim());
            }
            string CONT = ((TextBox)item.FindControl("txtCONT")).Text.Trim().ToUpper();
            string SEAL = ((TextBox)item.FindControl("txtSEAL")).Text.Trim().ToUpper();
            string ContType = ((TextBox)item.FindControl("txtContType")).Text.Trim().ToUpper();

            int VOL = 0;
            if (!string.IsNullOrEmpty(((TextBox)item.FindControl("txtVOL")).Text.Trim()))
            {
                try
                {
                    VOL = int.Parse(((TextBox)item.FindControl("txtVOL")).Text.Trim());
                }
                catch { }
            }
            string PKGS = ((TextBox)item.FindControl("txtPKGS")).Text.Trim().ToUpper();
            double GW = 0;
            if (!string.IsNullOrEmpty(((TextBox)item.FindControl("txtGW")).Text.Trim()))
            {
                try
                {
                    GW = double.Parse(((TextBox)item.FindControl("txtGW")).Text.Trim());
                }
                catch { }
            }
            double MEAS = 0;
            if (!string.IsNullOrEmpty(((TextBox)item.FindControl("txtMEAS")).Text.Trim()))
            {
                try
                {
                    MEAS = double.Parse(((TextBox)item.FindControl("txtMEAS")).Text.Trim());
                }
                catch { }
            }
            string TEMP = ((TextBox)item.FindControl("txtTEMP")).Text.Trim().ToUpper();

            ShippingDetail spd = new ShippingDetail();

            if (sdID > 0)
            {
                spd.ID = sdID;
                spd.Load(GV.cn, GV.trans);
            }
            else
            {
                spd.CusdocID = cusdocid;
            }
            if (CONT.Length > 0 || SEAL.Length > 0 || ContType.Length > 0 || PKGS.Length > 0 || GW > 0 || MEAS > 0)
            {
                spd.CONT = CONT;
                spd.ContType = ContType;
                spd.VOL = VOL;
                spd.GW = GW;
                spd.MEAS = MEAS;
                spd.PKGS = PKGS;
                spd.SEAL = SEAL;
                spd.TEMP = TEMP;

                spd.Save(GV.cn, GV.trans);
            }
        }
        //Save Tracking
        foreach (RepeaterItem item in rptTracking.Items)
        {

            int sdID = 0;
            if (!string.IsNullOrEmpty(((HiddenField)item.FindControl("txtID")).Value.Trim()))
            {
                sdID = int.Parse(((HiddenField)item.FindControl("txtID")).Value.Trim());
            }
            string LOCATION = ((TextBox)item.FindControl("txtLocation")).Text.Trim().ToUpper();
            string TRACKINGDAT = ((TextBox)item.FindControl("txtTrackingDate")).Text.Trim();
            string VSSL_TCK = ((TextBox)item.FindControl("txtVssl")).Text.Trim().ToUpper();
            string VOYNO_TCK = ((TextBox)item.FindControl("txtVoyno")).Text.Trim().ToUpper();
            int ETDAccept = 0;
            int ETAAccept = 0;
            CheckBox cbETDAccept = (CheckBox)item.FindControl("cbETDAccept");
            CheckBox cbETAAccept = (CheckBox)item.FindControl("cbETAAccept");
            if (cbETDAccept.Checked)
                ETDAccept = 1;
            if (cbETAAccept.Checked)
                ETAAccept = 1;
            string Discharge = ((TextBox)item.FindControl("txtDischarge")).Text.ToString();
            string ETA = ((TextBox)item.FindControl("txtETA")).Text.ToString();
            Tracking tck = new Tracking();

            if (sdID > 0)
            {
                tck.ID = sdID;
                tck.Load(GV.cn, GV.trans);
            }
            else
            {
                tck.CusdocID = cusdocid;
            }
            if (LOCATION.Length > 0)
            {
                Ports objPort = new Ports();
                if (LOCATION.Length > 0)
                {
                    objPort.Load(LOCATION, GV.cn, GV.trans);
                    if (objPort.ID > 0)
                        tck.LocationID = objPort.ID;
                }
                tck.Location = LOCATION;
                if (Discharge.Length > 0)
                {
                    objPort.Load(Discharge, GV.cn, GV.trans);
                    if (objPort.ID > 0)
                        tck.DischargeID = objPort.ID;
                }
                tck.Discharge = Discharge;
                //tck.TrackingDate = DateTime.Now;
                if (TRACKINGDAT.Length > 0 && TRACKINGDAT.ToUpper() != "TBA")
                {
                    tck.TrackingDate = DateTime.Parse(TRACKINGDAT, System.Globalization.CultureInfo.CreateSpecificCulture("en-CA"));
                }
                if (ETA.Length > 0 && ETA.ToUpper() != "TBA")
                {
                    tck.ETA = DateTime.Parse(ETA, System.Globalization.CultureInfo.CreateSpecificCulture("en-CA"));
                }
                tck.Vssl = VSSL_TCK;
                tck.Voyno = VOYNO_TCK;
                tck.ETDAccept = ETDAccept;
                tck.ETAAccept = ETAAccept;
                tck.Save(GV.cn, GV.trans);
            }
        }
        Response.Redirect("CreateCusdoc.aspx?ID=" + cusdocid.ToString());
    }
    private void LoadCusDoc()
    {
        if (Request.QueryString["ID"] != null)
        {
            long cusdocID = long.Parse(Request.QueryString["ID"].ToString());
            CusdocModel obj = new CusdocModel();
            obj.ID = cusdocID;
            obj.Load(GV.cn, GV.trans);
            txtFILENO.Text = obj.FILENO.ToString();
            txtMBKGNO.Text = obj.MBKGNO;
            txtHBLNO.Text = obj.HBLNO;
            txtMBLNO.Text = obj.MBLNO;
            txtHBLSHPR.Text = obj.HBLSHPR;
            txtHBLCNEE.Text = obj.HBLCNEE;
            txtHBLNTF.Text = obj.HBLNTF;
            txtDOGMN.Text = obj.DOGMN;
            txtTypeOfMovement.Text = obj.TypeOfMovement;
            if (obj.CARRCODE.Length > 0)
            {
                txtCARRCODE.Text = obj.CARRCODE;
                //txtCARRCODE.ReadOnly = true;
            }
            //st2
            txtVSSL.Text = obj.VSSL;
            txtVOYNO.Text = obj.VOYNO;
            txtPORID.Text = obj.PORNAME.ToString();//Display PORNAME
            txtPOLID.Text = obj.POLNAME.ToString();//Display POLNAME
            txtSHONBOARD.Text = obj.SHONBOARD.ToString("MMM-dd-yyyy");
            txtPODID.Text = obj.PODNAME.ToString();//Display PODNAME
            txtDESTID.Text = obj.DESTNAME.ToString();//Display DESTNAME
            txtSERIBILL.Text = obj.SERIBILL;
            //st3
            ddTOP.SelectedValue = obj.TOP;
            txtPAYABLEAT.Text = obj.PAYABLEATNAME;//Display Port Name
            txtPOISS.Text = obj.POISSNAME;//Display Port Name
            ddNOORI.Text = obj.NOORI;
            txtDOISS.Text = obj.DOISS.ToString("MMM-dd-yyyy"); ;
            txtAGDLVR.Text = obj.AGDLVR;
            txtCARRN.Text = obj.CARRN;

            //st4
            txtMBLSHPR.Text = obj.MBLSHPR;
            txtMBLCNEE.Text = obj.MBLCNEE;
            txtMBLNTF.Text = obj.MBLNTF;
            txtBOOKER.Text = obj.BOOKER;
            txtCTRNO.Text = obj.CTRNO.ToString();
            txtCTRVALID.Text = obj.CTRVALID.ToString("MMM-dd-yyyy"); ;
            txtCTRNAME.Text = obj.CTRNAME;

            //st5
            txtRATE.Text = obj.RATE.ToString("#.##");
            txtUNIT.Text = obj.UNIT;
            txtCURR.Text = obj.CURR;
            txtDVALUE.Text = obj.DVALUE.ToString("#,#.##");
            txtSTUFF.Text = obj.STUFF;
            txtNote.Text = obj.NOTE;
            ddTOPMBL.SelectedValue = obj.TOPMBL;
            if (obj.BACKDATE > DateTime.MinValue)
                txtBACKDATE.Text = obj.BACKDATE.ToString("MMM-dd-yyyy");
            cbMBLTYPE.SelectedValue = obj.MBLType.ToString();
            cbHBLTYPE.SelectedValue = obj.HBLType.ToString();
        }

    }
    private void BuildSPDList(string strCondition, int NumOfItemsPerPage, int PageNo, out int nNumOfPages, out DataTable dtList, MySqlConnection cn, MySqlTransaction trans)
    {
        DataSet ds = null;
        dtList = new DataTable();
        dtList.Columns.Add("ID");
        dtList.Columns.Add("INDEX");
        dtList.Columns.Add("CONT");
        dtList.Columns.Add("SEAL");
        dtList.Columns.Add("ContType");
        dtList.Columns.Add("VOL");
        dtList.Columns.Add("PKGS");
        dtList.Columns.Add("GW");
        dtList.Columns.Add("MEAS");
        dtList.Columns.Add("TEMP");
        dtList.Columns.Add("Action");
        nNumOfPages = 0;

        string[] strValues = null;
        if (Request.QueryString["ID"] != null)
        {
            strCondition += " AND CusdocID=" + Request.QueryString["ID"].ToString();

            ShippingDetail.GetList(strCondition, " ID ASC", NumOfItemsPerPage, PageNo, out nNumOfPages, out ds, GV.cn, GV.trans);

            if (ds != null && ds.Tables[0].Rows.Count > 0)
            {
                int count = 0;
                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    count++;
                    string ID = dr["ID"].ToString();
                    string strIndex = count.ToString();

                    string CONT = dr["CONT"].ToString();
                    string SEAL = dr["SEAL"].ToString();
                    string ContType = dr["ContType"].ToString();
                    string PKGS = dr["PKGS"].ToString();
                    string VOL = dr["VOL"].ToString();
                    if (VOL == "0") VOL = "";
                    string GW = "";
                    double dGW = double.Parse(dr["GW"].ToString());
                    if (dGW > 0)
                        GW = dGW.ToString("#,#.00");

                    string MEAS = "";
                    double dMEAS = double.Parse(dr["MEAS"].ToString());
                    if (dMEAS > 0)
                        MEAS = dMEAS.ToString("#,#.00");
                    if (!dr.IsNull("VOL"))
                        spdVOL_Sum += int.Parse(dr["VOL"].ToString());
                    spdGW_Sum += dGW;
                    spdMEAS_Sum += dMEAS;
                    string TEMP = dr["TEMP"].ToString();
                    string Action = " <a href=\"javascript:doDeleteSPR('" + Request.QueryString["ID"].ToString() + "','" + dr["ID"].ToString() + "')\">DEL</a>";

                    strValues = new string[] { ID, strIndex, CONT, SEAL, ContType, VOL, PKGS, GW, MEAS, TEMP, Action };
                    dtList.Rows.Add(strValues);
                }
            }
        }
        strValues = new string[] { "-1", "", "", "", "", "", "", "", "", "", "" };
        dtList.Rows.Add(strValues);
    }

    private void BuildTrackingList(string strCondition, int NumOfItemsPerPage, int PageNo, out int nNumOfPages, out DataTable dtList, MySqlConnection cn, MySqlTransaction trans)
    {
        DataSet ds = null;
        dtList = new DataTable();
        dtList.Columns.Add("ID");
        dtList.Columns.Add("Index");
        //dtList.Columns.Add("Status");
        dtList.Columns.Add("Location");
        dtList.Columns.Add("TrackingDate");
        dtList.Columns.Add("Vssl");
        dtList.Columns.Add("Voyno");

        dtList.Columns.Add("Discharge");
        dtList.Columns.Add("ETA");
        dtList.Columns.Add("ETDAccept");
        dtList.Columns.Add("ETAAccept");
        dtList.Columns.Add("ETDAcceptStatus");
        dtList.Columns.Add("ETAAcceptStatus");
        dtList.Columns.Add("Action");
        nNumOfPages = 0;
        string[] strValues = null;
        if (Request.QueryString["ID"] != null)
        {
            strCondition += " AND CusdocID=" + Request.QueryString["ID"].ToString();

            Tracking.GetList(strCondition, " ID ASC", NumOfItemsPerPage, PageNo, out nNumOfPages, out ds, GV.cn, GV.trans);
            if (ds != null && ds.Tables[0].Rows.Count > 0)
            {
                int count = 0;
                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    count++;
                    string ID = dr["ID"].ToString();
                    string strIndex = count.ToString();

                    //string Status = dr["Status"].ToString();
                    string Location = dr["Location"].ToString();
                    string TrackingDate = "TBA";
                    if (DateTime.Parse(dr["TrackingDate"].ToString()) != DateTime.MinValue)
                        TrackingDate = DateTime.Parse(dr["TrackingDate"].ToString()).ToString("dd/MM/yyyy");
                    string Vssl = dr["Vssl"].ToString();
                    string Voyno = dr["Voyno"].ToString();

                    string Discharge = dr["Discharge"].ToString();
                    string ETA = "TBA";
                    if (DateTime.Parse(dr["ETA"].ToString()) != DateTime.MinValue)
                        ETA = DateTime.Parse(dr["ETA"].ToString()).ToString("dd/MM/yyyy");
                    string ETDAccept = dr["ETDAccept"].ToString();
                    string ETAAccept = dr["ETAAccept"].ToString();

                    string ETDAcceptStatus = ETDAccept == "1" ? "ATD" : "ETD";
                    string ETAAcceptStatus = ETAAccept == "1" ? "ATA" : "ETA";
                    string Action = " <a href=\"javascript:doDeleteTracking('" + Request.QueryString["ID"].ToString() + "','" + ID + "')\">DEL</a>";

                    strValues = new string[] { ID, strIndex, 
                        Location, TrackingDate, Vssl, Voyno,
                        Discharge,
                        ETA,
                        ETDAccept,
                        ETAAccept,
                        ETDAcceptStatus,
                        ETAAcceptStatus,
                        Action };
                    dtList.Rows.Add(strValues);
                }
            }
        }
        strValues = new string[] { "-1", "", "", "", "", "", "", "" };
        dtList.Rows.Add(strValues);
    }
    protected void rptShippingDetail_ItemDataBound(Object sender, RepeaterItemEventArgs e)
    {
        DropDownList drd = (DropDownList)e.Item.FindControl("ddContType");
        TextBox txtContType = (TextBox)e.Item.FindControl("txtContType");
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            try
            {
                if (txtContType.Text.Length > 0)
                    drd.SelectedValue = txtContType.Text;
            }
            catch (Exception ex)
            {
                Response.Write(ex.ToString());
            }
            //Response.Write(hidVungID.Value+"<br>");

        }
    }
    protected void rptTracking_ItemDataBound(Object sender, RepeaterItemEventArgs e)
    {
        //DropDownList drd = (DropDownList)e.Item.FindControl("ddStatus");
        //HiddenField txtStatus = (HiddenField)e.Item.FindControl("txtStatus");
        //if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        //{
        //    try
        //    {
        //        if (txtStatus.Value.Length > 0)
        //            drd.SelectedValue = txtStatus.Value;
        //    }
        //    catch (Exception ex)
        //    {
        //        Response.Write(ex.ToString());
        //    }
        //    //Response.Write(hidVungID.Value+"<br>");

        //}
    }
}